package com.yazi.mxz.admin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.tool.PageLink;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.jackson.result.StatusResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.PrizeWin;
import com.yazi.mxz.common.entity.enums.PrizeType;
import com.yazi.mxz.common.entity.enums.PrizeWinStatus;
import com.yazi.mxz.common.service.PrizeWinService;

@Controller("prizeWinController")
@RequestMapping("/prizeWin")
public class PrizeWinController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(PrizeWinController.class);
	
	@Resource
	PrizeWinService prizeWinService;
	
	@RequestMapping("/list")
	public String list(PrizeWin prizeWin, Searching si, Model model) {
		log.debug("进入用户奖品列表页面");
		// 默认按时间倒序
		if (null == si.getSorts()) {
			List<Sort> sorts = new ArrayList<Sort>();
			sorts.add(new Sort("addTime", true));
			si.setSorts(sorts);
		}
		
		Page<PrizeWin> page = prizeWinService.list(prizeWin, si);
		// 分页链接
		PageLink pl = new MyPageLink(page.getPaging());
		// 奖品类型枚举
		PrizeType[] pt = PrizeType.values();
		// 奖品状态枚举
		PrizeWinStatus[] pws = PrizeWinStatus.values();
		
		model.addAttribute("prizeWinList", page.getList());
		model.addAttribute("pageLink", pl);
		model.addAttribute("searchWord", si.getSearchWord());
		model.addAttribute("prizeTypes", pt);
		model.addAttribute("prizeWinStatuses", pws);
		return "prizeWin_list";
	}
	
	@RequestMapping("/convertDo/json")
	@ResponseBody
	public StatusResult convertDoJson(Long winId) {
		log.debug("兑换奖品:winId={}", winId);
		StatusResult jsonResult = new StatusResult();
		try{
			prizeWinService.editToConvert(winId);
			jsonResult.setOkMessage("兑换成功");
		} catch (MyException e) {
			log.error("兑换奖品异常:", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		
		return jsonResult;
	}
	
}
